package com.lframework.xingyun.basedata.controller;

import com.lframework.xingyun.basedata.bo.productspu.GetProductSpuBo;
import com.lframework.xingyun.basedata.bo.productspu.QueryProductSpuBo;
import com.lframework.xingyun.basedata.vo.product.spu.QueryProductSpuVo;
import com.lframework.xingyun.basedata.service.product.ProductSpuService;
import com.lframework.xingyun.basedata.vo.product.spu.CreateProductSpuVo;
import com.lframework.xingyun.basedata.vo.product.spu.UpdateProductSpuVo;
import com.lframework.xingyun.basedata.entity.ProductSpu;

import javax.validation.constraints.NotBlank;
import com.lframework.starter.web.resp.InvokeResultBuilder;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.resp.PageResult;
import com.lframework.starter.web.resp.InvokeResult;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import com.lframework.starter.web.utils.PageResultUtil;
import io.swagger.annotations.Api;
import com.lframework.starter.web.controller.DefaultBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import com.lframework.starter.web.annotations.security.HasPermission;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品SPU Controller
 *
 * @author zhuchi
 */
@Api(tags = "商品SPU")
@Validated
@RestController
@RequestMapping("/basedata/productspu")
public class ProductSpuController extends DefaultBaseController {

    @Autowired
    private ProductSpuService productSpuService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"base-data:productspu:query"})
    @GetMapping("/query")
    public InvokeResult<PageResult<QueryProductSpuBo>> query(@Valid QueryProductSpuVo vo) {

        PageResult<ProductSpu> pageResult = productSpuService.query(getPageIndex(vo), getPageSize(vo), vo);

        List<ProductSpu> datas = pageResult.getDatas();
        List<QueryProductSpuBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryProductSpuBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"base-data:productspu:query"})
    @GetMapping
    public InvokeResult<GetProductSpuBo> get(@NotBlank(message = "id不能为空！") String id) {

        ProductSpu data = productSpuService.findById(id);
        if (data == null) {
            throw new DefaultClientException("商品SPU不存在！");
        }

        GetProductSpuBo result = new GetProductSpuBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"base-data:productspu:add"})
    @PostMapping
    public InvokeResult<Void> create(@Valid @RequestBody CreateProductSpuVo vo) {

        productSpuService.create(vo);

        return InvokeResultBuilder.success();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @HasPermission({"base-data:productspu:modify"})
    @PutMapping
    public InvokeResult<Void> update(@Valid @RequestBody UpdateProductSpuVo vo) {

        productSpuService.update(vo);

        productSpuService.cleanCacheByKey(vo.getId());

        return InvokeResultBuilder.success();
    }
}
